#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :


from threading import Thread, RLock

num = 0
lock_obj = RLock()


def add_():
    global num
    for i in range(1000000):
        with lock_obj:  # 使用上下文管理锁的申请与释放
            num += i


def sub_():
    global num
    for i in range(1000000):
        with lock_obj:
            num -= i


t1 = Thread(target=add_)
t2 = Thread(target=sub_)

t1.start()
t2.start()

t1.join()
t2.join()

print(num)


